<?php
namespace app\sysadmin\controller;
use think\Db;
class Contenttable extends Common{
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$_input_data = input('get.');
		$_get_key = array('name');
		$_get_str = "";

		if($_input_data){
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('get_str',$_get_str);
		return $this->fetch();
	}
	public function add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_data = array();
			$_data['name'] = $_post_data['name'];
			$_data['type_group'] = $_post_data['type_group'];
			$_data['category_tpl'] = $_post_data['category_tpl'];
			$_data['lists_tpl'] = $_post_data['lists_tpl'];
			$_data['show_tpl'] = $_post_data['show_tpl'];
			$_data['create_time'] = time();
			$_data['update_time'] = time();

			$_ret_id = Db::name('content_model_table')->insertGetId($_data);
			if($_ret_id){
				$this->add_operation_log($_ret_id,$_data);
				$this->create_table($_ret_id,$_data['name']);
				$this->success("添加成功",url('index'));
			}else{
				$this->error("添加失败");
			}
			exit;
		}

		$_info = array(
			'id'=>'0',
			'name'=>'',
			'type_group'=>'',
			'category_tpl'=>'',
			'lists_tpl'=>'',
			'show_tpl'=>''
		);
		$this->assign('info',$_info);
		$this->assign('page_name','添加模型');
		return $this->fetch('info');
	}
	public function edit(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_id = (int)$_post_data['id'];

			$_data = array();
			$_data['name'] = $_post_data['name'];
			$_data['type_group'] = $_post_data['type_group'];
			$_data['category_tpl'] = $_post_data['category_tpl'];
			$_data['lists_tpl'] = $_post_data['lists_tpl'];
			$_data['show_tpl'] = $_post_data['show_tpl'];
			$_data['update_time'] = time();

			$_ret_id = Db::name('content_model_table')->where('id','=',$_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				$_tables_name = config('database.prefix').'content_lists_'.$_id;
				Db::query("ALTER TABLE `".$_tables_name."` COMMENT='".$_data['name']."';");

				cache('sysadmin_get_table_info_'.$_id,null);
				cache('sysadmin_content_table',null);

				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('content_model_table')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		$this->assign('info',$_info);
		$this->assign('page_name','编辑模型');
		return $this->fetch('info');
	}
	public function del(){
		if($this->request->isGet() == true){
			$_id = (int)input('get.id');
			if(!$_id){
				$this->error("请选择需要删除的信息！");
			}
			$_info = Db::name('content_model_table')->where('id','=',$_id)->where('is_del','=','0')->find();
			if(!$_info){
				$this->error("请选择需要删除的信息！");
			}
			$_ret = Db::name('content_model_table')->where('id','=',$_id)->update(array('is_del',1));
			if($_ret){
				$this->add_operation_log($_id,$_info);

				cache('sysadmin_get_table_info_'.$_id,null);
				cache('sysadmin_content_table',null);

				$this->success("删除成功",url('index'));
			}else{
				$this->error("删除失败，请刷新以后重试！");
			}

		}else{
			$this->error("系统错误！");
		}
	}

	// 异步查询列表
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);
		$_where = array();
		if(!empty($_input_data['name'])){
			$_where[] = array('name','like','%'.$_input_data['name'].'%');
		}
		$_where[] = array('is_del','=','0');
		$count = Db::name('content_model_table')->where($_where)->count();
		$lists = Db::name('content_model_table')->where($_where)->page($page.','.$limit)->field('id,name,create_time')
					->order('id asc')->select();
		if($lists){
			$_content = new \Wy\Content;
			foreach($lists as $k=>$v){
				$_table_name = config('database.prefix').'content_lists_'.$v['id'];
				$lists[$k]['table_name'] = $_table_name;
				$lists[$k]['create_date'] = date('Y-m-d H:i:s',$v['create_time']);
				$lists[$k]['num'] = $_content->get_table_num($_table_name);
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}
	private function create_table($table_id,$name){
		// return;
		$_tables_name = config('database.prefix').'content_lists_'.$table_id;

		$_add_sql = "CREATE TABLE `".$_tables_name."` (
			`id` int(11) NOT NULL AUTO_INCREMENT,
			`title` varchar(200) NOT NULL COMMENT '标题',
			`cat_id` int(11) NOT NULL COMMENT '栏目id',
			`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '账户是否启用1启用 0禁用',
			`onclick` int(11) NOT NULL DEFAULT '0',
			`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',

			`seo_title` varchar(200) NOT NULL COMMENT 'SEO标题',
			`seo_keywords` varchar(200) NOT NULL COMMENT 'SEO关键词',
			`seo_description` varchar(500) NOT NULL COMMENT 'SEO描述',

			`push_date`  datetime NOT NULL DEFAULT '0',
			`create_time` int(11) NOT NULL DEFAULT '0',
			`update_time` int(11) NOT NULL DEFAULT '0',
			PRIMARY KEY (`id`)
		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='".$name."';";
		Db::query($_add_sql);

		cache('sysadmin_content_table',null);

		$_add_field = array();
		$_add_field[] = array(
			'table_id'		=> $table_id,
			'type_group_id' => '0',
			'field_name'	=> 'title',
			'cname'			=> '标题',
			'type'			=> 'input',
			'type'			=> '',//'字段类型',
			'tips'			=> '',//'提示信息',
			'sort'			=> '0',//'0',
			'status'		=> '1',//'1启用',
			'is_required'	=> '1',
			'is_nav'		=> '1',
			'is_search'		=> '1',
			'create_time'	=> time(),//'创建时间',
			'update_time'	=> time(),//'编辑时间',
		);
		$_add_field[] = array(
			'table_id'		=> $table_id,
			'type_group_id' => '99',
			'field_name'	=> 'seo_title',
			'cname'			=> 'SEO标题',
			'type'			=> 'input',
			'type'			=> '',//'字段类型',
			'tips'			=> '',//'提示信息',
			'sort'			=> '0',
			'status'		=> '1',
			'is_required'	=> '0',
			'is_nav'		=> '0',
			'is_search'		=> '0',
			'create_time'	=> time(),//'创建时间',
			'update_time'	=> time(),//'编辑时间',
		);
		$_add_field[] = array(
			'table_id'		=> $table_id,
			'type_group_id' => '99',
			'field_name'	=> 'seo_keywords',
			'cname'			=> 'SEO关键词',
			'type'			=> 'textarea',
			'type'			=> '',//'字段类型',
			'tips'			=> '',//'提示信息',
			'sort'			=> '0',
			'status'		=> '1',
			'is_required'	=> '0',
			'is_nav'		=> '0',
			'is_search'		=> '0',
			'create_time'	=> time(),//'创建时间',
			'update_time'	=> time(),//'编辑时间',
		);
		$_add_field[] = array(
			'table_id'		=> $table_id,
			'type_group_id' => '99',
			'field_name'	=> 'seo_description',
			'cname'			=> 'SEO描述',
			'type'			=> 'textarea',
			'type'			=> '',//'字段类型',
			'tips'			=> '',//'提示信息',
			'sort'			=> '0',
			'status'		=> '1',
			'is_required'	=> '0',
			'is_nav'		=> '0',
			'is_search'		=> '0',
			'create_time'	=> time(),//'创建时间',
			'update_time'	=> time(),//'编辑时间',
		);
		Db::name('content_model_field')->insertAll($_add_field);
	}
}
?>